home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 05.zip / BS1 part 5 / IM_3.adf / Exec / piarc.LZH / jpeg.rexx < prev    next >
OS/2 REXX Batch file  |  1992-06-22  |  9KB  |  379 lines

  1. /*
  2.  * JPEG.rexx
  3.  *
  4.  *  Written by: Ben Williams
  5.  * Last Update: March 4th, 1992
  6.  *         For: Black Belt Systems image processing series IM, IM F/c, and IP.
  7.  * ---------------------------------------------------------------------------
  8.  *    Revision: 1.07
  9.  */
  10. parse arg var1
  11. /*
  12.  * open rexxsupport.library -- needed for some functions
  13.  */
  14. if ~show('L',"rexxsupport.library") then do
  15.   if addlib('rexxsupport.library',0,-30,0) then do
  16.       /* everything's ok */
  17.     end;
  18.   else do
  19.     say 'We Have A Library Problem, Unable To Load "rexxsupport.library"';
  20.     say 'Cannot operate JPEG.rexx without this library - sorry!';
  21.     exit 10;
  22.     end;
  23.   end;
  24.  
  25. /*
  26.  * This will automatically direct the script to the proper
  27.  * software, if it is running.
  28.  */
  29. prtnme = 'IP_Port'; /* assume Image Professional */
  30. if show('P','IP_Port') = 0 then do
  31.   if show('P','IM_Port') = 0 then do
  32.     say "Can't find image processor's ARexx port!!!"; /* not running? */
  33.     say "This script requires IP, IM or IM F/c to run!";
  34.     exit(20);
  35.     end;
  36.   else do
  37.     prtnme = 'IM_Port'; /* That's the thing about assumptions... */
  38.     end;                 /* We make em, user's break em.          */
  39.   end;
  40.  
  41.   /*
  42.    * This code attempts to read a file called "picmdpath" from REXX:
  43.    * If it can't find it, the script will assume that the commands
  44.    * associated with this PI Module are in "c:". If the file exists,
  45.    * the script will look in the path that is specified in the file.
  46.    * If you create this file, you MUST put a complete, correct path
  47.    * in it; if the commands are in a sub-directory, you have to put
  48.    * the trailing slash on the path (like, device:dir/).
  49.    * 
  50.    */
  51.   cmdpath = 'c:';
  52.   if open(fhandle,'rexx:picmdpath','read') then  /* open the file */
  53.     do
  54.       cmdpath = readln(fhandle);
  55.       call close(fhandle);  /* close the file    */
  56.     end
  57.  
  58. /*
  59.  * Prompt user - load, or save?
  60.  */
  61. if var1 = 'load' then do
  62.   pick = 1
  63.   end
  64. else if var1 = 'save' then do
  65.   pick = 2
  66.   end
  67. else do
  68.     address(prtnme);
  69.     'tofront';
  70.     options results;
  71.     'gadgets "Load","JFIF/JPEG","Save","JFIF/JPEG"';
  72.     pick = result;
  73.     options;
  74.     address;
  75.     end;
  76.  
  77. if pick=0 then do
  78.   exit 0;
  79.   end;
  80.  
  81. /*
  82.  * PI driver for compressor
  83.  */
  84. if pick=2 then do /* compression */
  85.  
  86.   bufname = 'image';
  87.   strn = ' -q ';
  88.   address(prtnme);
  89.  
  90.   options results;
  91.   'askprop "Compression/Damage Factor","0","0","100"';
  92.   qfact = result;
  93.   'askyn "Good compression","Best Compression"';
  94.   compr = result;
  95.   options;
  96.  
  97.   if compr = 1 then do
  98.     strn = ' -E'||strn;
  99.     end;
  100.  
  101.   prevpath = 'ram:'; /* put user in ram to start with... */
  102.   if show('C',jpegpath) = 1 then do
  103.     prevpath = getclip(jpegpath);
  104.     end;
  105.  
  106.   options results;
  107.   'current';
  108.   bufdata = result; /* get name of buffer, if there is one */
  109.   parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
  110.   if bname ~= '<none>' then do
  111.     bufname = bname;
  112.     end;
  113.   if (length(bufname) > 4) then do
  114.     epos = pos('.jpg',bufname,length(bufname)-4);
  115.     if epos ~= 0 then do
  116.       bufname = left(bufname,epos-1)
  117.       end
  118.     end;
  119.   'filerequest "'||prevpath||'","'||bufname||'",".jpg","Save JPEG"';
  120.   jpegfile = result;
  121.   options;
  122.  
  123.   if jpegfile = 'FR_CANCELLED' then do
  124.     address(prtnme);
  125.     'imtofront';
  126.     exit 0;
  127.     end;
  128.  
  129. jpegfile = expandfilename(jpegfile); /* make filename complete path */
  130. thispath = gimmepath(jpegfile);
  131. call setclip(jpegpath,thispath);
  132.  
  133.   address(prtnme);
  134.   options results;
  135.   'jackin';
  136.   jackadr = result;
  137.   options;
  138.  
  139.   'wbtofront';
  140.   address command cmdpath||'wrjpg >"'||jpegfile||'" -j'||jackadr||strn||qfact;
  141.  
  142.   address(prtnme);
  143.   'imtofront';
  144.   address;
  145.  
  146.   exit 0;
  147.  
  148.   end;
  149.  
  150. else do /* decompression */
  151.  
  152. /*
  153.  * Setup default path
  154.  */
  155.   prevpath = 'ram:'; /* put user in ram to start with... */
  156. /*
  157.  * Now, get old path if it exists
  158.  */
  159.   if show('C',jpegpath) = 1 then do
  160.     prevpath = getclip(jpegpath);
  161.     end;
  162.  
  163.   address(prtnme);
  164.   options results;
  165.   'current';
  166.   bufdata = result; /* get name of buffer, if there is one */
  167.   parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
  168.   if bname ~= '<none>' then do
  169.     bufname = bname;
  170.     end;
  171.   if (length(bufname) > 4) then do
  172.     epos = pos('.jpg',bufname,length(bufname)-4);
  173.     if epos ~= 0 then do
  174.       bufname = left(bufname,epos-1)
  175.       end
  176.     end;
  177.   'filerequest "'||prevpath||'","'||bufname||'",".jpg","Load JPEG"';
  178.   jpegfile = result;
  179.   options;
  180.  
  181.   if jpegfile = 'FR_CANCELLED' then do
  182.     address(prtnme);
  183.     'imtofront';
  184.     exit 0;
  185.     end;
  186.  
  187.   jpegfile = expandfilename(jpegfile);
  188.   thispath = gimmepath(jpegfile);
  189.   call setclip(jpegpath,thispath);
  190.  
  191.   fileinfo = statef(jpegfile);
  192.   parse var fileinfo fitype fibytes fiblocks fiflags fidays fimins fiticks ficomment;
  193.  
  194.   if fitype = '' then do
  195.     'message Cannot locate "'jpegfile'" for processing';
  196.     exit 10;
  197.     end;
  198.  
  199.   if fibytes < 170 then do
  200.     "message This file is too small to be a JFIF formatted JPEG file";
  201.     exit 10;
  202.     end;
  203.  
  204.   if fitype = 'DIR' then do
  205.     'message Must specify a file, not a directory';
  206.     exit 10;
  207.     end;
  208.  
  209. /*
  210.  * at this point, we have at least some assurance that we
  211.  * have a real JPEG file to work with. Now, we need to look into
  212.  * the file and see how big the image is, so we can open a new
  213.  * buffer of the appropriate size.
  214.  */
  215.  
  216.   call open(fhandle,jpegfile,'read');      /* open the file     */
  217.   if rvalue() ~= 65496 then do /* FFD8 */
  218.     'message Not a JFIF file! - Initial read fails';
  219.     call close(fhandle);
  220.     exit(0);
  221.     end;
  222.  
  223.   work=1;
  224.   do while work = 1
  225.     thisid = rvalue();
  226.     thisln = rvalue();
  227.     thisda = readch(fhandle,thisln-2);
  228.     select
  229.       when thisid = 65472 | thisid < 65488 then do /* FFC0-FFCF (SOF0-15) */
  230.         height = c2d(substr(thisda,2,2));
  231.         width  = c2d(substr(thisda,4,2));
  232.         work=0;
  233.         end;
  234.       when thisid = 65504 then do  /* FFE0 */
  235.         fid = left(thisda,4);
  236.         if fid ~= 'JFIF' then do
  237.           'message Not a JFIF file!';
  238.           call close(fhandle);
  239.           exit 0;
  240.           end;
  241.         end;
  242.       otherwise do
  243.         nop;
  244.         end;
  245.       end;
  246.     end;
  247.  
  248.   call close(fhandle);                     /* close the file    */
  249.  
  250.   if height < 0 then do
  251.     "message Bad height: "||height;
  252.     exit 0;
  253.     end;
  254.  
  255.   if height > 32767 then do
  256.     "message Bad height: "||height;
  257.     exit 0;
  258.     end;
  259.  
  260.   if width < 0 then do
  261.     "message Bad width: "||width;
  262.     exit 0;
  263.     end;
  264.  
  265.   if width > 32767 then do
  266.     "message Bad width: "||width;
  267.     exit 0;
  268.     end;
  269.  
  270.   address(prtnme);
  271.   
  272.   'imtofront'; /* show user the IM screen */
  273.   /* is there already a primary buffer??? */
  274.   options results;
  275.   'current';
  276.   bufdata = result;
  277.   options;
  278.  
  279.   parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum
  280.   if bname ~= '<none>' then do
  281.     address(prtnme);
  282.     options results;
  283.     'askyn '||'"Replace Primary ['||bname||']" "New As Primary"'
  284.     prefs = result;
  285.     options;
  286.     address;
  287.     if prefs = 0 then do
  288.       address(prtnme);
  289.       'killbuff '||bnum; /* this kills the Primary Buffer */
  290.       address;
  291.     end;
  292.   end;
  293.  
  294.   /* New buffer is created at current resolution */
  295.   address(prtnme);
  296.   'autoredraw 0';
  297.   options results;
  298.   'newbuf '||width||' '||height;
  299.   if rc ~= 0 then do
  300.     "message Can't allocate buffer!";
  301.     'autoredraw 1';
  302.     exit 0;
  303.     end
  304.   bnum = result;
  305.   'rename '||bnum||' 'gxname;
  306.   'newcurrent '||bnum;
  307.   address;
  308.  
  309.   address(prtnme);
  310.   options results;
  311.   'jackin';
  312.   jackadr = result;
  313.   options;
  314.  
  315.   'wbtofront';
  316.   'lockimage '||bnum;
  317.   address command cmdpath||'rdjpg -j'||jackadr||' "'||jpegfile||'"';
  318.   'unlockimage '||bnum;
  319.  
  320.   address(prtnme);
  321.   'imtofront';
  322.   'redraw';
  323.   address;
  324.  
  325.   exit 0;
  326.  
  327. end;
  328.  
  329. /*
  330.  * gimmepath
  331.  *
  332.  * This takes the provided argument and sucks the path out of it, then
  333.  * returns that path to the caller, sans file name.
  334.  */
  335. gimmepath:
  336.   arg fullnamegx;
  337.     tempgx = reverse(fullnamegx);
  338.     lengx = length(fullnamegx);   /* get length of string */
  339.     slashdex = index(tempgx,'/'); /* first occurance of '/' from right */
  340.     colondex = index(tempgx,':');  /* first occurance of ':' from right */
  341.     seploc = 0; /* assumes current dir, no path supplied */
  342.     if slashdex ~= 0 then do /* we assume we are in a DIR */
  343.       seploc = (lengx - slashdex)+1;
  344.       end;
  345.     else do
  346.       if colondex ~= 0 then do /* we assume we are on a device */
  347.         seploc = (lengx - colondex)+1;
  348.         end;
  349.       end;
  350.   gxname = substr(fullnamegx,seploc+1); /* if you ever need it */
  351.   gxpath = left(fullnamegx,seploc);
  352.   return(gxpath);
  353.  
  354. /*
  355.  * Since this script can't be expected to know where the CD of the user
  356.  * is when this cmd is invoked, we have to check the path the user
  357.  * provides - if it's not specified right from a root, then we have
  358.  * to make it a complete specification from the root.
  359.  */
  360. expandfilename:
  361.   parse arg jfile;
  362.   if index(jfile,':') = 0 then do
  363.     curdir = pragma(D);
  364.     if right(curdir,1) ~= ':' then do
  365.       if right(curdir,1) ~= '/' then do
  366.         if curdir ~= '' then do
  367.           curdir = curdir || '/';
  368.           end;
  369.         end;
  370.       end;
  371.     jfile = curdir||jfile;
  372.     end;
  373.   return(jfile);
  374.  
  375. rvalue:
  376.   wordnum = c2d(readch(fhandle,1)) * 256;
  377.   wordnum = wordnum + c2d(readch(fhandle,1));
  378.   return wordnum;
  379.